//pack11jsclass

var SearchBlock = new FormObject("SearchForm");

function FormObject(id) {

    this.element=id;
    this.selects=new Array();
    this.currentSelectFound=false;
    this.nextElementDefined=true;
    this.nextElement="";
    this.currentElement="";

    this.getElement = function() {
        try {
            this.element = document.getElementById(this.element);
            allSelects = this.element.getElementsByTagName("select");
        } catch(e) { return false; }
        for(as=0; as<allSelects.length;as++) {
            idPair=allSelects[as].id.split("_");
            if(idPair[1]=="select") {
                this.selects.push(allSelects[as]);
            }
        }
    }

    this.rebuildSelects = function(_from) {
        //trace(_from.id);
        for(i=0; i<this.selects.length; i++) {
            _select=this.selects[i];

            mainID=_select.id.split("_");
            lamp=document.getElementById(mainID[0]+"_lamp");

            sel=new ajaxVar;
            sel.name=_select.name;
            sel.value=_select.value;
            AjaxManager.variables.push(sel.query());
            if(this.currentSelectFound && _select && _select!=null) {
                _select.disabled=true;
                lamp.className=(this.nextElementDefined)? "loader":"locked";
                if(this.nextElementDefined) this.nextElement=_select;
                this.nextElementDefined=false;
            } else {
                _select.disabled=false;
                lamp.className="free";
            }
            if(_from==_select) {
                this.currentElement=_from;
                this.currentSelectFound=true;
            }
        }
        this.currentSelectFound=false;
        this.nextElementDefined=true;
        //trace(this.nextElement.id);

        cur=new ajaxVar;
        cur.name="current";
        cur.value=this.nextElement.name;
        AjaxManager.variables.push(cur.query());

        AjaxManager.url="?response=SearchSelectsResponder";
        AjaxManager.backHandler=this.ajaxHandler;
        AjaxManager.callServer();
   }

   this.ajaxHandler = function(text,xml) {
            //Trace(text);
           blockIDArray=SearchBlock.nextElement.id.split("_");
           //var ne = document.getElementById(SearchBlock.nextElement.id);
           //if(text!="<!-- 0 -->") {
           // Trace(SearchBlock.nextElement.name+" "+SearchBlock.nextElement.options.length);
            //SearchBlock.nextElement.innerHTML=text;
            var opts=text.split("</option>");
            SearchBlock.nextElement.options.length=0;
            for(i=0; i<opts.length-1; i++) {
                if(opts[i]!="") {
                    var oData=opts[i].split('"');
                    var oID=oData[1];
                    var oValue=opts[i].split(">")[1];
                    var oSelected=(opts[i].indexOf("selected")>=0)? true:false;
                    SearchBlock.nextElement.options[i]=new Option(oValue,oID,oSelected);
                }
            }
            //SearchBlock.nextElement.outerHTML=SearchBlock.nextElement.outerHTML.replace("</select>",text+"</select>");
            //SearchBlock.nextElement=document.getElementById(SearchBlock.nextElement.id);
            //Trace(SearchBlock.nextElement.name+" "+SearchBlock.nextElement.options.length);
           if(SearchBlock.nextElement.options.length>0) {
            document.getElementById(blockIDArray[0]).style.display="block";
            document.getElementById(blockIDArray[0]+"_lamp").className="free";
            SearchBlock.nextElement.disabled=false;
           } else {
            document.getElementById(blockIDArray[0]).style.display="none";
           }
       if(SearchBlock.nextElement!=SearchBlock.selects[SearchBlock.selects.length-1]) {
           SearchBlock.rebuildSelects(SearchBlock.nextElement);
       }

       /*trace(text);
       if(SearchBlock.nextElement!=SearchBlock.selects[SearchBlock.selects.length-1]) {
           blockIDArray=SearchBlock.nextElement.id.split("_");
           if(text!="") {
            trace(SearchBlock.nextElement.name);
            SearchBlock.nextElement.innerHTML=text;
            document.getElementById(blockIDArray[0]).style.display="block";
            document.getElementById(sel[0]+"_lamp").className="free";
            SearchBlock.nextElement.disabled=false;
           } else {
            document.getElementById(blockIDArray[0]).style.display="none";
           }
            SearchBlock.rebuildSelects(SearchBlock.nextElement);
       } else {
           sel=SearchBlock.nextElement.id.split("_");
           document.getElementById(sel[0]+"_lamp").className="free";
           SearchBlock.nextElement.disabled=false;
       }*/
   }

   this.searchAgain=function(link) {
       tempForm=document.getElementById("SearchForm");
       tempForm.action=link;
       tempForm.submit();
   }

    this.setShowPrice=function(element) {
        spHidden=document.getElementById(element.id+"_value");
        spHidden.value=(element.checked==true)? 1:0;
        TablePropertiesPopup.changedElements.push(spHidden);
    }

    StartUpManager.addFunction("SearchBlock","getElement");
}

function f(o){
	try {
        o.value=o.value.replace(/([^0-9])/g,"");
	}	catch(e) {
		Trace("Form: FORM "+e);
	}
    //alert(searchForm.element);
}
